package com.bbm.communitysystem.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bbm.communitysystem.core.domain.R;
import com.bbm.communitysystem.domain.Staff;
import com.bbm.communitysystem.domain.dto.StaffDto;
import com.bbm.communitysystem.service.impl.StaffServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bbm
 * @since 2025-02-28
 */
@Tag(name = "员工管理")
@RestController
@RequestMapping("/staff")
@RequiredArgsConstructor
public class StaffController {

    private final StaffServiceImpl staffService;


    @Operation(summary = "新增员工")
    @PostMapping
    public R<String> addStaff(@RequestBody StaffDto staffDto){

        Staff staff = BeanUtil.copyProperties(staffDto, Staff.class);

        staff.setCreateTime(System.currentTimeMillis() + "");
        staff.setUpdateTime(staff.getCreateTime());
        staff.setCreateBy(staffDto.getUpdateBy());

        boolean save = staffService.save(staff);

        return save ? R.ok("新增成功") : R.error("新增失败");
    }


    @Operation(summary = "查询所有员工")
    @GetMapping
    @Parameter(name = "page", description = "页码")
    @Parameter(name = "size", description = "每页数量")
    public R<List<Staff>> getAllStaff(@RequestParam("page") Integer page, @RequestParam("size") Integer size) {
        Page<Staff> staffPage = new Page<>(page, size);
        staffService.page(staffPage);
        return R.ok(staffPage.getRecords());
    }

    @Operation(summary = "查询单个员工")
    @GetMapping("/{id}")
    @Parameter(name = "id", description = "员工id")
    public R<Staff> getStaffById(@PathVariable Long id) {
        return R.ok(staffService.getById(id));
    }

    @Operation(summary = "修改员工信息")
    @PutMapping
    public R<String> updateStaff(@RequestBody StaffDto staffDto) {

        if (staffDto.getUpdateBy() == null) {
            return R.error("请填写修该人id");
        }

        boolean update = staffService.lambdaUpdate()
                .set(staffDto.getStaffName() != null && !staffDto.getStaffName().isEmpty(), Staff::getStaffName, staffDto.getStaffName())
                .set(staffDto.getStaffContact() != null && !staffDto.getStaffContact().isEmpty() ,Staff::getStaffContact, staffDto.getStaffContact())

                .set(Staff::getUpdateBy, staffDto.getUpdateBy())
                .set(Staff::getUpdateTime, System.currentTimeMillis() + "")
                .eq(Staff::getId, staffDto.getId())
                .update();

        return update ? R.ok("修改成功") : R.error("修改失败");
    }

    @Operation(summary = "删除员工")
    @DeleteMapping("/{id}")
    @Parameter(name = "id", description = "员工id")
    public R<String> deleteStaff(@PathVariable Long id) {
        boolean b = staffService.removeById(id);
        return b ? R.ok("删除成功") : R.error("删除成功");
    }

}
